package com.soil.code.generation.database.mysql.repository;

import com.soil.code.generation.database.constant.JavaTypeEnum;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.HashMap;
import java.util.Map;

/**
 * @author zeno
 */
public class MysqlColumTypeMapper{

  private static final Map<String,String> DATA_TYPE_MAP = new HashMap<>();

  static {
    // 整型
    DATA_TYPE_MAP.put("tinyint",JavaTypeEnum.Byte.toString());
    DATA_TYPE_MAP.put("smallint",JavaTypeEnum.Integer.toString());
    DATA_TYPE_MAP.put("mediumint",JavaTypeEnum.Integer.toString());
    DATA_TYPE_MAP.put("int",JavaTypeEnum.Integer.toString());
    DATA_TYPE_MAP.put("bigint",JavaTypeEnum.Long.toString());

    // 无符号整型
    DATA_TYPE_MAP.put("tinyint unsigned",JavaTypeEnum.Short.toString());
    DATA_TYPE_MAP.put("smallint unsigned",JavaTypeEnum.Integer.toString());
    DATA_TYPE_MAP.put("mediumint unsigned",JavaTypeEnum.Integer.toString());
    DATA_TYPE_MAP.put("int unsigned",JavaTypeEnum.Long.toString());
    DATA_TYPE_MAP.put("bigint unsigned",JavaTypeEnum.BigInteger.toString());

    // 浮点型
    DATA_TYPE_MAP.put("float",JavaTypeEnum.Float.toString());
    DATA_TYPE_MAP.put("double",JavaTypeEnum.Double.toString());
    DATA_TYPE_MAP.put("decimal",JavaTypeEnum.Double.toString());

    // 无符号浮点型
    DATA_TYPE_MAP.put("float unsigned",JavaTypeEnum.BigDecimal.toString());
    DATA_TYPE_MAP.put("double unsigned",JavaTypeEnum.BigDecimal.toString());
    DATA_TYPE_MAP.put("decimal unsigned",JavaTypeEnum.BigDecimal.toString());

    // 日期时间型
    DATA_TYPE_MAP.put("datetime",JavaTypeEnum.LocalDateTime.toString());
    DATA_TYPE_MAP.put("timestamp",JavaTypeEnum.LocalDateTime.toString());
    DATA_TYPE_MAP.put("date",JavaTypeEnum.LocalDate.toString());
    DATA_TYPE_MAP.put("time",JavaTypeEnum.LocalTime.toString());
    DATA_TYPE_MAP.put("year",JavaTypeEnum.Short.toString());
    // 字符串类型
    DATA_TYPE_MAP.put("set",JavaTypeEnum.String.toString());
    DATA_TYPE_MAP.put("enum",JavaTypeEnum.String.toString());
    DATA_TYPE_MAP.put("char",JavaTypeEnum.String.toString());
    DATA_TYPE_MAP.put("varchar",JavaTypeEnum.String.toString());
    DATA_TYPE_MAP.put("tinytext",JavaTypeEnum.String.toString());
    DATA_TYPE_MAP.put("mediumtext",JavaTypeEnum.String.toString());
    DATA_TYPE_MAP.put("text",JavaTypeEnum.String.toString());
    DATA_TYPE_MAP.put("longtext",JavaTypeEnum.String.toString());
    DATA_TYPE_MAP.put("json",JavaTypeEnum.String.toString());
    // 二进制类型
    DATA_TYPE_MAP.put("bit",JavaTypeEnum.ByteArray.toString());
    DATA_TYPE_MAP.put("binary",JavaTypeEnum.ByteArray.toString());
    DATA_TYPE_MAP.put("varbinary",JavaTypeEnum.ByteArray.toString());
    DATA_TYPE_MAP.put("blob",JavaTypeEnum.ByteArray.toString());
    DATA_TYPE_MAP.put("tinyblob",JavaTypeEnum.ByteArray.toString());
    DATA_TYPE_MAP.put("mediumblob",JavaTypeEnum.ByteArray.toString());
    DATA_TYPE_MAP.put("longblob",JavaTypeEnum.ByteArray.toString());
  }

  public static String getJavaType(String columnType){
    String typeString = DATA_TYPE_MAP.get(columnType);
    if( typeString == null ){
      return "Object";
    }
    return typeString;
  }
}
